﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;

namespace YQGL.Common
{
    public class BreviaryImg
    {

        /// <summary>
        /// 生成缩略图
        /// </summary>
        /// <param name="imgPath">原图片路径</param>
        /// <param name="sytImgPath">缩略图路径</param>
        /// <param name="ImgWidth">缩略图宽</param>
        /// <param name="ImgHeight">缩略图高</param>
        /// <param name="mode">生成缩略图形式</param>
        /// <param name="isWater">是否生成水印</param>
        /// <param name="WaterCls">水印类型</param>
        /// <param name="strEntityNo">公司编号</param>
        /// <param name="strWaterMarkPos">水印位置 如无则填string.Empty</param>
        /// <param name="LeftRightMargin">左右边距</param>
        /// <param name="TopBottomMargin">上下边距</param>
        public static void SetImg(string imgPath, string sytImgPath, int ImgWidth, int ImgHeight, int mode, int isWater, int WaterCls, string strWaterMarkPos, int LeftRightMargin, int TopBottomMargin)
        {
            //imgPath = HttpContext.Current.Server.MapPath(imgPath);

            System.Drawing.Image originalImage = System.Drawing.Image.FromFile(imgPath);

            int x = 0;
            int y = 0;
            int ow = originalImage.Width;
            int oh = originalImage.Height;

            switch (mode)
            {
                case 1://按比例

                    double dow = double.Parse(ow.ToString());
                    double doh = double.Parse(oh.ToString());
                    double imgW = double.Parse(ImgWidth.ToString());
                    double imgH = double.Parse(ImgHeight.ToString());
                    double bl = 0;
                    if (ImgWidth == 0 || ImgHeight == 0)
                    {
                        bl = dow / doh;
                    }
                    else
                    {
                        bl = Math.Max(dow / doh, imgW / imgH);
                    }
                    imgW = imgW * bl;
                    imgH = imgH * bl;

                    string strw = imgW.ToString();
                    string strh = imgH.ToString();
                    if (strw.IndexOf(".") != -1)
                    {
                        ImgWidth = Int32.Parse(strw.Substring(0, strw.IndexOf(".")));
                    }
                    else
                    {
                        ImgWidth = Int32.Parse(strw);
                    }

                    if (strh.IndexOf(".") != -1)
                    {
                        ImgHeight = Int32.Parse(strh.Substring(0, strh.IndexOf(".")));
                    }
                    else
                    {
                        ImgHeight = Int32.Parse(strh);
                    }

                    break;
                case 2://指定高宽缩放（可能变形）                      
                    break;
                default://原始大小
                    ImgWidth = Int32.Parse(ow.ToString());
                    ImgHeight = Int32.Parse(oh.ToString());
                    break;
            }



            //新建一个bmp图片
            System.Drawing.Image bitmap = new System.Drawing.Bitmap(ImgWidth, ImgHeight);


            //新建一个画板

            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);

            //设置高质量插值法
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

            //设置高质量,低速度呈现平滑程度
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

            //清空画布并以透明背景色填充

            g.Clear(System.Drawing.Color.Transparent);

            //在指定位置并且按指定大小绘制原图片的指定部分
            g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, ImgWidth, ImgHeight),
            new System.Drawing.Rectangle(x, y, ow, oh),
            System.Drawing.GraphicsUnit.Pixel);




            //if (isWater == 1)//加水印 
            //{
            //    bitmap = getImgSyt(bitmap, strEntityNo, WaterCls, strWaterMarkPos, LeftRightMargin, TopBottomMargin);
            //}


            try
            {
                //以jpg格式保存缩略图

                bitmap.Save(sytImgPath, System.Drawing.Imaging.ImageFormat.Jpeg);

            }
            catch (System.Exception e)
            {
                throw e;
            }
            finally
            {
                originalImage.Dispose();
                bitmap.Dispose();
                g.Dispose();
            }

        }


        /// <summary>
        /// 生成缩略图(返回缩略图二进制值)
        /// </summary>
        /// <param name="imgPath">原图片路径</param>
        /// <param name="sytImgPath">缩略图路径</param>
        /// <param name="ImgWidth">缩略图宽</param>
        /// <param name="ImgHeight">缩略图高</param>
        /// <param name="mode">生成缩略图形式</param>
        public static byte[] setImg1(string imgPath, string sytImgPath, int ImgWidth, int ImgHeight, int mode)
        {

            imgPath = HttpContext.Current.Server.MapPath(imgPath);

            System.Drawing.Image originalImage = System.Drawing.Image.FromFile(imgPath);

            int x = 0;
            int y = 0;
            int ow = originalImage.Width;
            int oh = originalImage.Height;

            switch (mode)
            {
                case 1://按比例

                    double dow = double.Parse(ow.ToString());
                    double doh = double.Parse(oh.ToString());
                    double imgW = double.Parse(ImgWidth.ToString());
                    double imgH = double.Parse(ImgHeight.ToString());
                    double bl = 0;
                    if (ImgWidth == 0 || ImgHeight == 0)
                    {
                        bl = dow / doh;
                    }
                    else
                    {
                        bl = Math.Max(dow / doh, imgW / imgH);
                    }
                    imgW = imgW * bl;
                    imgH = imgH * bl;

                    string strw = imgW.ToString();
                    string strh = imgH.ToString();
                    if (strw.IndexOf(".") != -1)
                    {
                        ImgWidth = Int32.Parse(strw.Substring(0, strw.IndexOf(".")));
                    }
                    else
                    {
                        ImgWidth = Int32.Parse(strw);
                    }

                    if (strh.IndexOf(".") != -1)
                    {
                        ImgHeight = Int32.Parse(strh.Substring(0, strh.IndexOf(".")));
                    }
                    else
                    {
                        ImgHeight = Int32.Parse(strh);
                    }

                    break;
                case 2://指定高宽缩放（可能变形）                      
                    break;
                default:
                    break;
            }



            //新建一个bmp图片
            System.Drawing.Image bitmap = new System.Drawing.Bitmap(ImgWidth, ImgHeight);


            //新建一个画板

            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);

            //设置高质量插值法
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

            //设置高质量,低速度呈现平滑程度
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

            //清空画布并以透明背景色填充

            g.Clear(System.Drawing.Color.Transparent);

            //在指定位置并且按指定大小绘制原图片的指定部分
            g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, ImgWidth, ImgHeight),
            new System.Drawing.Rectangle(x, y, ow, oh),
            System.Drawing.GraphicsUnit.Pixel);
            try
            {
                //以jpg格式保存缩略图
                bitmap.Save(HttpContext.Current.Server.MapPath(sytImgPath), System.Drawing.Imaging.ImageFormat.Jpeg);
                //获取二进制
                MemoryStream ms = new MemoryStream();
                bitmap.Save(ms, ImageFormat.Jpeg);
                byte[] buffer = ms.ToArray();
                ms.Close();
                return buffer;
            }
            catch (System.Exception e)
            {
                throw e;
            }
            finally
            {
                originalImage.Dispose();
                bitmap.Dispose();
                g.Dispose();
            }

        }

        /// <summary>
        /// 生成缩略图(不返回任何数据)
        /// </summary>
        /// <param name="imgPath">原图片路径</param>
        /// <param name="sytImgPath">缩略图路径</param>
        /// <param name="ImgWidth">缩略图宽</param>
        /// <param name="ImgHeight">缩略图高</param>
        /// <param name="mode">生成缩略图形式</param>
        public static void setImg2(string imgPath, string sytImgPath, int ImgWidth, int ImgHeight, int mode)
        {

            imgPath = HttpContext.Current.Server.MapPath(imgPath);

            System.Drawing.Image originalImage = System.Drawing.Image.FromFile(imgPath);

            int ow = originalImage.Width;
            int oh = originalImage.Height;

            int flat = 0;//标记图片是不是等比
            int xPoint = 0;//若果要补白边的话，原图像所在的x，y坐标。    
            int yPoint = 0;
            float New_Width = 0; // 新的宽度    
            float New_Height = 0; // 新的高度 

            switch (mode)
            {
                case 1://按比例

                    float dow = float.Parse(ow.ToString());
                    float doh = float.Parse(oh.ToString());
                    float imgW = float.Parse(ImgWidth.ToString());
                    float imgH = float.Parse(ImgHeight.ToString());

                    if (dow < imgW && doh < imgH)//图片的高和宽都不超出范围
                    {
                        New_Width = dow;
                        New_Height = doh;
                        xPoint = (ImgWidth - ow) / 2;
                        yPoint = (ImgHeight - oh) / 2;
                        flat = 1;
                    }
                    else if ((dow / doh) > (imgW / imgH)) //当图片太宽的时候    
                    {
                        New_Height = doh * ((float)imgW / (float)dow);
                        New_Width = (float)imgW;
                        //此时x坐标不用修改    
                        yPoint = (int)(((float)imgH - New_Height) / 2);
                        flat = 1;
                    }
                    else if ((dow / doh) == (imgW / imgH))
                    {
                        New_Width = imgW;
                        New_Height = imgH;
                    }
                    else
                    {
                        New_Width = dow * (imgH / doh);  //太高的时候   
                        New_Height = imgH;
                        //此时y坐标不用修改    
                        xPoint = (int)(((float)imgW - New_Width) / 2);
                        flat = 1;
                    }

                    //double bl = 0;
                    //if (ImgWidth == 0 || ImgHeight == 0)
                    //{
                    //    bl = dow / doh;
                    //}
                    //else
                    //{
                    //    bl = Math.Max(dow / doh, imgW / imgH);
                    //}
                    //imgW = imgW * bl;
                    //imgH = imgH * bl;

                    //string strw = imgW.ToString();
                    //string strh = imgH.ToString();
                    //if (strw.IndexOf(".") != -1)
                    //{
                    //    ImgWidth = Int32.Parse(strw.Substring(0, strw.IndexOf(".")));
                    //}
                    //else
                    //{
                    //    ImgWidth = Int32.Parse(strw);
                    //}

                    //if (strh.IndexOf(".") != -1)
                    //{
                    //    ImgHeight = Int32.Parse(strh.Substring(0, strh.IndexOf(".")));
                    //}
                    //else
                    //{
                    //    ImgHeight = Int32.Parse(strh);
                    //}

                    break;
                case 2://指定高宽缩放（可能变形）
                    New_Width = ImgWidth;
                    New_Height = ImgHeight;
                    break;
                default:
                    break;
            }



            //新建一个bmp图片
            System.Drawing.Image bitmap = new System.Drawing.Bitmap(ImgWidth, ImgHeight);


            //新建一个画板

            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);

            //设置高质量插值法
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

            //设置高质量,低速度呈现平滑程度
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

            //清空画布并以透明背景色填充

            g.Clear(System.Drawing.Color.Transparent);

            //在指定位置并且按指定大小绘制原图片的指定部分
            if (flat == 0)//指定高度缩放或等比
            {
                //g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, ImgWidth, ImgHeight),
                //           new System.Drawing.Rectangle(x, y, ow, oh),
                //           System.Drawing.GraphicsUnit.Pixel);
                g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, ImgWidth, ImgHeight),
                           new System.Drawing.Rectangle(xPoint, yPoint, ow, oh),
                           System.Drawing.GraphicsUnit.Pixel);
            }
            else
            {
                SolidBrush tbBg = new SolidBrush(Color.White);
                g.FillRectangle(tbBg, 0, 0, ImgWidth, ImgHeight); //填充为白色
                //g.DrawImage(originalImage, xPoint, yPoint, (int)New_Width, (int)New_Height);
                g.DrawImage(originalImage, new System.Drawing.Rectangle(xPoint, yPoint, (int)New_Width, (int)New_Height),
                           new System.Drawing.Rectangle(0, 0, ow, oh),
                           System.Drawing.GraphicsUnit.Pixel);

            }

            try
            {
                //以jpg格式保存缩略图
                bitmap.Save(HttpContext.Current.Server.MapPath(sytImgPath), System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            catch (System.Exception e)
            {
                throw e;
            }
            finally
            {
                originalImage.Dispose();
                bitmap.Dispose();
                g.Dispose();
            }

        }



        ///// <summary>
        ///// 图片加水印
        ///// </summary>
        ///// <param name="image">image对象</param>
        ///// <param name="strEntityNo">公司编号</param>
        ///// <param name="waterCls">水印类型</param>
        ///// <param name="strWaterMarkPos">水印位置，如无则填string.Empty</param>
        ///// <param name="LeftRightMargin">左右边距</param>
        ///// <param name="TopBottomMargin">上下边距</param>
        ///// <returns></returns>
        //public static System.Drawing.Image getImgSyt(System.Drawing.Image image,, int waterCls, string strWaterMarkPos, int LeftRightMargin, int TopBottomMargin)
        //{
        //    BLL.Entity entityBll = new BLL.Entity();
        //    Model.Entity entityObject = entityBll.GetModel(strEntityNo);
        //    string strWaterImgPath = entityObject.FWaterMarkSvrFileName;//缩略图片路径
        //    string strText = entityObject.FTxtWaterMark;//文字

        //    if (waterCls == 1)//图片水印
        //    {
        //        string Path_sypf = HttpContext.Current.Server.MapPath(strWaterImgPath);//水印图片路径
        //        System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Path_sypf);
        //        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
        //        int x = image.Width - copyImage.Width;
        //        int y = image.Height - copyImage.Height;

        //        if (strWaterMarkPos != string.Empty)//有水印位置，设置路径
        //        {
        //            List<int> listint = getXY(strWaterMarkPos, LeftRightMargin, TopBottomMargin, image.Width, image.Height, copyImage.Width, copyImage.Height);
        //            x = listint[0];
        //            y = listint[1];
        //        }
        //        g.DrawImage(copyImage, new System.Drawing.Rectangle(x, y, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel);
        //        g.Dispose();
        //        copyImage.Dispose();
        //    }
        //    else
        //    {
        //        Graphics g = Graphics.FromImage(image);
        //        g.DrawImage(image, 0, 0, image.Width, image.Height);
        //        Font f = new Font("宋体", 20, GraphicsUnit.Pixel);
        //        Brush b = new SolidBrush(Color.White);

        //        SizeF size = g.MeasureString(strText, f);
        //        float textWidth = size.Width; //字符串像素宽度

        //        float textHeight = size.Height;//字符串像素高度

        //        float x = image.Width - textWidth;
        //        float y = image.Height - textHeight;
        //        if (strWaterMarkPos != string.Empty)//有水印位置，设置路径
        //        {
        //            List<float> listfloat = getXYFloat(strWaterMarkPos, LeftRightMargin, TopBottomMargin, image.Width, image.Height, textWidth, textHeight);
        //            x = listfloat[0];
        //            y = listfloat[1];
        //        }
        //        g.DrawString(strText, f, b, x, y);
        //        g.Dispose();
        //    }

        //    return image;
        //}


        public static List<int> getXY(string strWaterMarkPos, int lefright, int topbottom, int imgWidth, int imgHeight, int sltWidth, int sltHeight)
        {
            int x = 0;
            int y = 0;
            List<int> listInt = new List<int>();
            switch (strWaterMarkPos)
            {
                case "1":
                    {
                        x = lefright;
                        y = topbottom;
                    } break;
                case "2":
                    {
                        x = (imgWidth - sltWidth) / 2;
                        y = topbottom;
                    } break;
                case "3":
                    {
                        x = imgWidth - sltWidth - lefright;
                        y = topbottom;
                    } break;
                case "4":
                    {
                        x = lefright;
                        y = (imgHeight - sltHeight) / 2;
                    } break;
                case "5":
                    {
                        x = (imgWidth - sltWidth) / 2;
                        y = (imgHeight - sltHeight) / 2;
                    } break;
                case "6":
                    {
                        x = imgWidth - sltWidth - lefright;
                        y = (imgHeight - sltHeight) / 2;
                    } break;
                case "7":
                    {
                        x = lefright;
                        y = imgHeight - sltHeight - topbottom;
                    } break;
                case "8":
                    {
                        x = (imgWidth - sltWidth) / 2;
                        y = imgHeight - sltHeight - topbottom;
                    } break;
                case "9":
                    {
                        x = imgWidth - sltWidth - lefright;
                        y = imgHeight - sltHeight - topbottom;
                    } break;
            }

            listInt.Add(x);
            listInt.Add(y);
            return listInt;
        }


        public static List<float> getXYFloat(string strWaterMarkPos, int lefright, int topbottom, float imgWidth, float imgHeight, float sltWidth, float sltHeight)
        {
            float x = 0;
            float y = 0;
            List<float> listfloat = new List<float>();
            switch (strWaterMarkPos)
            {
                case "1":
                    {
                        x = lefright;
                        y = topbottom;
                    } break;
                case "2":
                    {
                        x = (imgWidth - sltWidth) / 2;
                        y = topbottom;
                    } break;
                case "3":
                    {
                        x = imgWidth - sltWidth - lefright;
                        y = topbottom;
                    } break;
                case "4":
                    {
                        x = lefright;
                        y = (imgHeight - sltHeight) / 2;
                    } break;
                case "5":
                    {
                        x = (imgWidth - sltWidth) / 2;
                        y = (imgHeight - sltHeight) / 2;
                    } break;
                case "6":
                    {
                        x = imgWidth - sltWidth - lefright;
                        y = (imgHeight - sltHeight) / 2;
                    } break;
                case "7":
                    {
                        x = lefright;
                        y = imgHeight - sltHeight - topbottom;
                    } break;
                case "8":
                    {
                        x = (imgWidth - sltWidth) / 2;
                        y = imgHeight - sltHeight - topbottom;
                    } break;
                case "9":
                    {
                        x = imgWidth - sltWidth - lefright;
                        y = imgHeight - sltHeight - topbottom;
                    } break;
            }

            listfloat.Add(x);
            listfloat.Add(y);
            return listfloat;
        }




    }
}